package com.testitems.service.apiaddrconfig;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.testitems.common.web.page.PageInfo;
import com.testitems.config.security.UserInfo;
import com.testitems.controller.config.apiaddr.dto.ApiAddrAddRequest;
import com.testitems.controller.config.apiaddr.dto.ApiAddrSearchRequest;
import com.testitems.controller.config.apiaddr.dto.ApiAddrUpdateRequest;
import com.testitems.repo.entity.ApiAddrConfig;
import com.testitems.repo.mapper.ApiAddrConfigMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 接口地址配置 服务实现类
 * </p>
 *
 * @author GFH
 * @since 2021-11-23
 */
@Service
public class ApiAddrConfigServiceImpl extends ServiceImpl<ApiAddrConfigMapper, ApiAddrConfig> implements IApiAddrConfigService {

    @Autowired
    private ApiAddrConfigMapper addrConfigMapper;

    @Override
    public PageInfo<ApiAddrConfig> search(UserInfo userInfo, ApiAddrSearchRequest request) {
        Page<ApiAddrConfig> page = new Page<>(request.getCurrent(), request.getSize());
        LambdaQueryWrapper<ApiAddrConfig> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(request.getName())) {
            queryWrapper.like(ApiAddrConfig::getName, request.getName());
        }
        queryWrapper.eq(ApiAddrConfig::getIsDeleted, false);
        Page<ApiAddrConfig> selectPage = addrConfigMapper.selectPage(page, queryWrapper);
        return PageInfo.get(selectPage);
    }

    @Override
    public ApiAddrConfig getApiAddrConfigByName(String name) {
        LambdaQueryWrapper<ApiAddrConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiAddrConfig::getName, name);
        queryWrapper.eq(ApiAddrConfig::getIsDeleted, false);
        return addrConfigMapper.selectOne(queryWrapper);
    }

    @Override
    public List<ApiAddrConfig> getApiAddrList() {
        LambdaQueryWrapper<ApiAddrConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApiAddrConfig::getIsDeleted,false);
        return addrConfigMapper.selectList(queryWrapper);

    }

    @Override
    public void add(UserInfo userInfo, ApiAddrAddRequest request) {
        ApiAddrConfig apiAddrConfig = new ApiAddrConfig();
        BeanUtils.copyProperties(request, apiAddrConfig);
        apiAddrConfig.setCreatedById(userInfo.getId());
        apiAddrConfig.setCreatedBy(userInfo.getName());
        apiAddrConfig.setCreatedTime(new Date());
        addrConfigMapper.insert(apiAddrConfig);
    }

    @Override
    public void updateById(Integer id, UserInfo userInfo, ApiAddrUpdateRequest request) {
        ApiAddrConfig apiAddrConfig = super.getById(id);
        LambdaUpdateWrapper<ApiAddrConfig> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApiAddrConfig::getId, id);
        apiAddrConfig.setName(request.getName());
        apiAddrConfig.setUrl(request.getUrl());
        if (StringUtils.isBlank(request.getRemark())) {
            updateWrapper.set(ApiAddrConfig::getRemark, null);
        } else {
            apiAddrConfig.setRemark(request.getRemark());
        }
        apiAddrConfig.setUpdatedById(userInfo.getId());
        apiAddrConfig.setUpdatedBy(userInfo.getName());
        apiAddrConfig.setUpdatedTime(new Date());
        addrConfigMapper.update(apiAddrConfig, updateWrapper);
    }

    @Override
    public void logicDelete(UserInfo userInfo, Integer id) {
        ApiAddrConfig apiAddrConfig = super.getById(id);
        apiAddrConfig.setIsDeleted(true);
        apiAddrConfig.setUpdatedBy(userInfo.getName());
        apiAddrConfig.setUpdatedById(userInfo.getId());
        apiAddrConfig.setUpdatedTime(new Date());
        addrConfigMapper.updateById(apiAddrConfig);
    }
}
